package com.chookss.mine.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.studyPlan.StudyPlanAddActivity;
import com.chookss.mine.entity.MyViedeoEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.Utils;
import com.chookss.video.entity.VideoEntity;
import com.chookss.video.recommend.VedioDetailActivity;
import com.chookss.view.MyRoundLayout;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class MyLongVideoAdapter extends BaseQuickAdapter<MyViedeoEntity.VideoListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private int employeeCodeType;//0自己的主页1别人的主页
    private String courseCode, courseName;

    public MyLongVideoAdapter(int layoutResId, Context context, @Nullable List<MyViedeoEntity.VideoListBean> data, int employeeCodeType) {
        super(layoutResId, data);
        this.context = context;
        this.employeeCodeType = employeeCodeType;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 42)) / 2;
        height = width * 110 / 183;
    }

    public void setData(String courseCode,String courseName) {
        this.courseCode=courseCode;
        this.courseName=courseName;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, MyViedeoEntity.VideoListBean item) {
        MyRoundLayout rl = helper.getView(R.id.item_rl);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rl.getLayoutParams();
        params.height = height;
        rl.setLayoutParams(params);
        TextView tvTiltle = helper.getView(R.id.tvTiltle);
        TextView tvNumber = helper.getView(R.id.tvNumber);
        TextView tvTime = helper.getView(R.id.tvTime);
        ImageView ivBg = helper.getView(R.id.ivBg);
        tvTiltle.setText(item.getVideoTitle());
        tvTime.setText(Utils.changeTime(item.getVideoSeconds()));
        tvNumber.setText(Utils.changeCounts(item.getVideoPlayCounts()) + "次播放");
        GlideUtils.load(context, item.getVideoCoverPath(), ivBg, R.drawable.default_vedio);
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoEntity video = Utils.modelA2B(item);
                if (video == null) {
                    return;
                }
                Intent intent = new Intent(context, VedioDetailActivity.class);
                intent.putExtra("video", video);
                context.startActivity(intent);
            }
        });
        helper.getItemView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (Utils.isNull(courseCode)) {
                    addVideo(item);
                } else {
                    addDialog(item);
                }

                return true;
            }
        });
    }

    private void addVideo(MyViedeoEntity.VideoListBean videoEntity) {
        new android.app.AlertDialog.Builder(context)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent(context, StudyPlanAddActivity.class);
                        intent.putExtra("title", videoEntity.getVideoTitle());
                        intent.putExtra("describe", videoEntity.getVideoKeyWord());
                        intent.putExtra("resourceId", videoEntity.getVideoId());
                        intent.putExtra("time", videoEntity.getVideoSeconds());
                        intent.putExtra("picUrl", videoEntity.getVideoCoverPath());
                        intent.putExtra("resourceType", 2);
                        context.startActivity(intent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setMessage("确定要视频加入学习任务么?")
                .setTitle("提示")
                .show();

    }

    private int index2 = -1;

    private void addDialog(MyViedeoEntity.VideoListBean videoEntity) {
        final String[] array = new String[]{"视频加入学习任务", "课程加入学习任务"};
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setSingleChoiceItems(array, -1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        index2 = which;
                    }
                });
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);
        builder.setTitle("提示");
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(),"保存",Toast.LENGTH_SHORT).show();
                if (index2 == -1) {
                    MyToast.show("选择加入类型");
                    return;
                } else if (index2 == 0) {
                    index2 = -1;
                    dialog.dismiss();
                    Intent intent = new Intent(context, StudyPlanAddActivity.class);
                    intent.putExtra("title", videoEntity.getVideoTitle());
                    intent.putExtra("describe", videoEntity.getVideoKeyWord());
                    intent.putExtra("resourceId", videoEntity.getVideoId());
                    intent.putExtra("time", videoEntity.getVideoSeconds());
                    intent.putExtra("picUrl", videoEntity.getVideoCoverPath());
                    intent.putExtra("resourceType", 2);
                    context.startActivity(intent);
                } else if (index2 == 1) {
                    index2 = -1;
                    dialog.dismiss();
                    Intent intent = new Intent(context, StudyPlanAddActivity.class);
                    intent.putExtra("title", courseName);
                    intent.putExtra("describe", videoEntity.getVideoKeyWord());
                    intent.putExtra("resourceId", courseCode);
                    intent.putExtra("resourceType", 4);
                    context.startActivity(intent);
                }
            }
        });
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index2 = -1;
                dialog.dismiss();
            }
        });
    }
}
